<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Layout\Content;
use Encore\Admin\Facades\Admin;
use App\Modules\Medicinal\Services\MedicinalService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;


class MedicinalController extends Controller
{
    /**
     * 药品列表
     */

    public function medicinalIndex(Request $request){
        $param = $request->all();
        $result = MedicinalService::getList($param);

        return Admin::content(function (Content $content) use($result){

            $content->body(view("Medicinal.index",compact('result')));
        });
    }

    /**
     * 添加或者修改药品
     */
    public function insertMedicinal(Request $request){
        $param = $request->all();

        $data['medicinal_cn'] = $param['medicinal_cn'];
        $data['medicinal_en'] = $param['medicinal_en'];
        $data['insulin_preparation'] = $param['preparation'];
        $data['tag'] = $param['tag'];
        $data['insulin_type'] = $param['type'];
        $data['image'] = $param['image'];

        if(!empty($param['id'])){//跟新
            $result = MedicinalService::upMedicinal($param['id'],$data);
            if ( empty($result) ) return response()->error("UPDATE_FAILURE");

        }else {//添加

            $result = MedicinalService::insertMedicinal($data);
            if ( empty($result) ) return response()->error("ADD_FAILURE");

        }
        return response()->success();
    }

    /**
     * 获取药品
     */
    public function oneMedicinal(Request $request){
        $id = $request->input('id',0);
        if ( empty($id) ) return response()->error("PARAM_EMPTY");
        return response()->success(MedicinalService::oneMedicinal($id,'id'));
    }


    //删除列表
    public function deletMedicinal(Request $request){

        $param = $request->all();
        $id = $param['id'];
        $del['status'] = 0;

        if(empty($param['id'])) return  response()->error("PARAM_EMPTY");

        $result = MedicinalService::upMedicinal($id,$del);
        if ( empty($result) ) return response()->error("DELETE_PRODUCT_FAILED");

        return response()->success();
    }

    /**
     * 获取药品接口
     */
    public function medicinalList(){
        return response()->success(MedicinalService::medicinalList());
    }

}